package com.example.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.config.R;
import com.example.domain.Power;
import com.example.mapper.PowerMapper;
import com.example.param.DistributeParam;
import com.example.service.IPowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.PipedWriter;
import java.util.List;

/**
 * 权限Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-22
 */
@Service
public class PowerServiceImpl implements IPowerService {

    @Autowired
    private PowerMapper powerMapper;

    @Override
    public R distribute(DistributeParam distributeParam) {
        List<String> departmentName = distributeParam.getDepartmentName();
        LambdaQueryWrapper<Power> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Power::getDealerId, distributeParam.getDealerId());
        Power power = powerMapper.selectOne(lqw);
        if (power == null) {
            power = new Power();
            power.setDealerId(distributeParam.getDealerId());
            if (departmentName.contains("hydrothermalBusinessDepartment")) {
                power.setHydrothermalBusinessDepartment(1L);
            }
            if (departmentName.contains("kitchenElectricityBusinessDepartment")) {
                power.setKitchenElectricityBusinessDepartment(1L);
            }
            if (departmentName.contains("thermalEnergyBusinessDepartment")) {
                power.setThermalEnergyBusinessDepartment(1L);
            }
            if (departmentName.contains("airEnergyBusinessDepartment")) {
                power.setAirEnergyBusinessDepartment(1L);
            }
            if (departmentName.contains("changshaEngineeringDepartment")) {
                power.setChangshaEngineeringDepartment(1L);
            }
            if (departmentName.contains("shundeEngineeringDepartment")) {
                power.setShundeEngineeringDepartment(1L);
            }
            powerMapper.insert(power);
        } else {
            if (departmentName.contains("hydrothermalBusinessDepartment")) {
                power.setHydrothermalBusinessDepartment(1L);
            }
            if (departmentName.contains("kitchenElectricityBusinessDepartment")) {
                power.setKitchenElectricityBusinessDepartment(1L);
            }
            if (departmentName.contains("thermalEnergyBusinessDepartment")) {
                power.setThermalEnergyBusinessDepartment(1L);
            }
            if (departmentName.contains("airEnergyBusinessDepartment")) {
                power.setAirEnergyBusinessDepartment(1L);
            }
            if (departmentName.contains("changshaEngineeringDepartment")) {
                power.setChangshaEngineeringDepartment(1L);
            }
            if (departmentName.contains("shundeEngineeringDepartment")) {
                power.setShundeEngineeringDepartment(1L);
            }
            powerMapper.updateById(power);
        }
        return R.fail("分配成功");
    }

    @Override
    public R getPower(Long dealerId) {
        LambdaQueryWrapper<Power> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Power::getDealerId, dealerId);
        Power power = powerMapper.selectOne(lqw);
        if (power == null) {
            return R.fail("该账户没有权限");
        }
        return R.ok(power);
    }
}
